package tmi.li.txwidgets;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


public class TxWidgetsPref {
	public static final String TAG ="tmi.li.txwidgets.TxWidgetsPref";
	public static final String PREFS_NAME_TXCLCK = "TxClckPref";
	public static final String PREFS_NAME_TXWTHR = "TxWthrPref";
	public static final String PREFS_NAME_TXBTRY = "TxBtryPref";
	public static final String FLCOLOR = "TxWidgetsLeftColor";
	public static final String FTCOLOR = "TxWidgetsTopColor";
	public static final String FBCOLOR = "TxWidgetsBottomColor";
	public static final String BCOLOR = "TxWidgetsBackgroundColor";
	public static final String LANG = "TxWidgetsLanguage";
	public static final String CURRENT = "TxWidgetsCurrent";
	public static final String DESC = "TxWidgetsDesc";
	public static final String DESC2 = "TxWidgetsDesc2";
	public static final String APPNAME = "TxWidgetsAppName";
	public static final String WOEID = "TxWidgetsWoeid";
	public static final String FORMAT = "TxWidgetsFormat";
	public static final String TFORMAT = "TxWidgetsTimeFormat";
	public static final String BTRYLEVEL = "TxWidgetsBatteryLevel";
	public static final String BTRYCLEVEL = "TxWidgetsBatteryCurrentLevel";
	public static final String BTRYPLUG = "TxWidgetsPlug";
	public static final String BTRYTIME = "TxWidgetsTime";
	public static final String BTRYRTIME = "TxWidgetsRestTime";
	public static final String SIZE = "TxWidgetsSize";
	public static final String CURRENTCREATED = "TxWidgetsCurrentlyCreated";
	
	private static final String[] MISC = {  FLCOLOR, 
											FTCOLOR, 
											FBCOLOR, 
											BCOLOR, 
											APPNAME,
											SIZE,
											
											//mainly used for txwthr
											CURRENT, 
											DESC, 
											DESC2,
											LANG, 
											WOEID,
											
											//used by txwthr and txclck (12/24h;celcius/fahrenheit)
											FORMAT,
											TFORMAT,
											
											//mainly used for txbtry 
											BTRYLEVEL,
											BTRYCLEVEL,
											BTRYPLUG,
											BTRYTIME,
											BTRYRTIME 
											
											};
	
	
	
	private String PREFS_NAME;
	
	public TxWidgetsPref(String pref) {
		PREFS_NAME = pref;
	}
	
	public void saveMisc(Context context, int id, String what, String value) {
		Log.d(TAG, "saveMisc() id => "+id+" | what => "+what+" | value => "+value);
		try {
			SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
			prefs.putString(id+what, value);
			prefs.commit();
		}catch(Exception e) {
			Log.d(TAG, "saveMisc() failed");
		}
	}
	
	public String getMisc(Context context, int id, String what) {
		Log.d(TAG, "getMisc() widget => "+PREFS_NAME+" | id => "+id+" | what => "+what);
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String prefix = prefs.getString(id+what, null);    
        if(prefix != null) {
        	Log.d(TAG, "getMisc() returning "+prefix);
        	return prefix;     	
        }else {
        	Log.d(TAG, "getMisc() returning null");
        	return null;
        }
	}

	public static String getCurrent(Context context) {
		SharedPreferences prefs = context.getSharedPreferences("TxWidgets", 0);
		String prefix = prefs.getString("TxWidgets", null);    
        if(prefix != null) {
        	return prefix;     	
        }else {
        	return null;
        }
	}
	
	public static void saveCurrent(Context context, int id, String widget) {
		SharedPreferences.Editor prefs = context.getSharedPreferences("TxWidgets", 0).edit();
		prefs.putString("TxWidgets", widget+";"+id);
		prefs.commit();
	}
	
	
	public void deleteMisc(Context context, int id) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
		for(int i = 0; i < MISC.length; i++) {
			try {
				prefs.remove(id+MISC[i]);
			}catch(Exception e){
				
			}
		}
		prefs.commit();
	}
	
	
	
	
	
}
